using System.Collections.Generic;
using System.Collections.ObjectModel;
using DslCrossModelFramework.ModelTraversal;
using Microsoft.VisualStudio.Modeling;

namespace DslCrossModelFramework.ModelRelationship {
    /// <summary>
    /// Common base class for element filters
    /// </summary>
    public abstract class ModelElementFilterBase : IModelElementFilter {

        /// <summary>
        /// Filters the collection.
        /// </summary>
        /// <param name="collectionToFilter">The collection to filter.</param>
        /// <returns></returns>
        public virtual IList<ModelElement> FilterCollection(IEnumerable<ModelElement> collectionToFilter) {
            List<ModelElement> filteredCollection = new List<ModelElement>();
            foreach (ModelElement element in collectionToFilter) {
                if (IsValid(element)) {
                    filteredCollection.Add(element);
                }
            }
            return new ReadOnlyCollection<ModelElement>(filteredCollection);
        }

        /// <summary>
        /// Determines whether the specified model element is valid for this filter.
        /// </summary>
        /// <param name="modelElement">The model element.</param>
        /// <returns>
        /// 	<c>true</c> if the specified model element is valid for this filter; otherwise, <c>false</c>.
        /// </returns>
        public abstract bool IsValid(ModelElement modelElement);

        /// <summary>
        /// Determines whether the specified path segment is valid.
        /// </summary>
        /// <param name="pathSegment">The path segment.</param>
        /// <returns>
        /// 	<c>true</c> if the specified model segment is valid for this filter; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool IsValid(LinkedListNode<IModelPathSegment> pathSegment) {
            return this.IsValid(pathSegment.Value.ModelElement);
        }
    }
}